import java.util.Date;
import java.util.Scanner;
import java.text.SimpleDateFormat;

public class TicketManager {
    private static SessionManager sessionManager = new SessionManager();
    private static Scanner scanner = new Scanner(System.in);

    public void sellTicket(User receptionist) {
        System.out.print("请输入场次ID: ");
        int sessionId = getValidIntegerInput();
        Session session = sessionManager.findSessionById(sessionId);
        if (session == null) {
            System.out.println("场次不存在");
            return;
        }

        sessionManager.showSessionSeats(sessionId);

        System.out.print("请输入座位号(如3排5号请输入3-5): ");
        String seat = scanner.nextLine();
        if (sessionManager.isSeatSold(sessionId, seat)) {
            System.out.println("该座位已售出");
            return;
        }

        double price = session.getPrice();
        Date sessionTime = session.getTime();
        java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.setTime(sessionTime);
        int hour = cal.get(java.util.Calendar.HOUR_OF_DAY);
        if (hour < 10) {
            price *= 0.9;
            System.out.println("早场优惠9折");
        }

        Ticket ticket = new Ticket(session.getMovieName(), session, seat, price);
        sessionManager.addTicket(ticket);

        System.out.println("售票成功！");
        System.out.println("票号: " + ticket.getTicketId());
        System.out.println("影片: " + ticket.getSession().getMovieName());
        System.out.println("时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm").format(ticket.getSession().getTime()));
        System.out.println("座位: " + ticket.getSeat());
        System.out.println("票价: " + ticket.getPrice());

        logOperation(receptionist.getUsername(), "售出票" + ticket.getTicketId(), new Date());
    }

    public void refundTicket(User receptionist) {
        System.out.print("请输入票号: ");
        String ticketId = scanner.nextLine();

        System.out.println("请输入场次ID: ");
        int sessionId = getValidIntegerInput();
        Session session = sessionManager.findSessionById(sessionId);
        if (session == null) {
            System.out.println("场次不存在");
            return;
        }

        System.out.println("请输入座位号(如3排5号请输入3-5): ");
        String seat = scanner.nextLine();
        if (!sessionManager.isSeatSold(sessionId, seat)) {
            System.out.println("票不存在");
            return;
        }

        Date now = new Date();
        Date sessionTime = session.getTime();
        long diffMillis = sessionTime.getTime() - now.getTime();
        long diffHours = diffMillis / (1000 * 60 * 60);

        double refundAmount = session.getPrice();
        if (diffHours < 2) {
            refundAmount *= 0.8;
            System.out.println("距离开演不足2小时，收取20%手续费");
        }

        sessionManager.removeTicket(ticketId);
        System.out.println("退票成功！");
        System.out.println("退款金额: " + refundAmount);

        logOperation(receptionist.getUsername(), "退掉票" + ticketId, new Date());
    }

    private int getValidIntegerInput() {
        while (true) {
            try {
                return Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的整数");
            }
        }
    }

    private void logOperation(String operator, String action, Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String log = sdf.format(time) + " - 操作人: " + operator + " - 操作: " + action;
        System.out.println("[日志] " + log);
    }
}
